
import java.util.Scanner;
import java.util.regex.Pattern;
import java.io.*;


public class GS {
    private static int nbrOfPairs;
    private static int[][] preferences;
    private static String[] names;

    public static void main(String[] args) {

	System.out.println("args[0] " + args[0] + " args[1] " + args[1]);
	Scanner sc = new Scanner(args[0]);
	sc.useDelimiter("n=");
	sc.next();
	sc.reset();
	sc.skip("n=");
	nbrOfPairs = sc.nextInt();
	preferences = new int[nbrOfPairs*2 + 1][nbrOfPairs + 1];
	names = new String[nbrOfPairs*2 + 1];
	while(sc.hasNextInt()) {
	    names[sc.nextInt()] = sc.next();
	}
	//sc.useDelimiter(" |: | \n|\n");
	sc.useDelimiter(": *|\\s*[\n]|[\\s\\n\\t]+|\\s*[ \t\n\f\r\u2028\u2029\u0085]+| \n"); //Use delimiter whitespace or colon plus zero or more whitespaces
	String line = sc.nextLine();
	while(sc.hasNextInt()) {
	    int person = sc.nextInt();
	    System.out.println(person + ": ");
	    for(int i = 1; i <= nbrOfPairs; i++) {
		preferences[person][i] = Integer.parseInt(sc.next());
		System.out.print(preferences[person][i] + " ");
	    }
	}

	boolean proposalAccepted;
	for(int man = man(); man != 0; man = man()){
	    //System.out.println("man " + man);
	    proposalAccepted = false;
	    for(int i = 1; i <= nbrOfPairs && !proposalAccepted; i++) {
		//System.out.println(preferences[man][i]);
		if(preferences[man][i] > 0) {
		    proposalAccepted = propose(man, i);
		}
	    }
	}
	
	printResult(args[1]);
    }

    private static boolean propose(int man, int womanIndex) {
	//System.out.println("propose");
	int woman = preferences[man][womanIndex];
        if(indexOf(woman, man) < indexOf(woman, preferences[woman][0]) ||
	   preferences[woman][0] == 0) {
		preferences[man][0] = woman;
		preferences[preferences[woman][0]][0] = 0;
		preferences[woman][0]  = man;
		preferences[man][womanIndex] = preferences[man][womanIndex] * -1;
		//System.out.println(man + " - " + woman);
		return true;
	}
	return false;
    }

	private static int indexOf(int p1, int p2) {
	    for(int i = 1; i <= nbrOfPairs; i++) {
		if(preferences[p1][i] == p2) {
		    return i;
		}
	    }
	    return 0;
	}
    
    private static int man() {
	for(int i = 1; i <= nbrOfPairs*2; i+=2) {
	    if(preferences[i][0] == 0) {
		return i;
	    }
	}
	return 0;
    }

    private static void printResult(String fileName) {
	try {
        StringBuilder sb = new StringBuilder();
	for(int i = 1; i <= nbrOfPairs*2; i+=2) {
	    System.out.println(names[i] + " -- " + names[preferences[i][0]]);
	    sb.append(names[i] + " -- " + names[preferences[i][0]]);
	}
	FileInputStream is = new FileInputStream(fileName);
	BufferedReader input = new BufferedReader(new InputStreamReader(is));
	StringBuilder sb2 = new StringBuilder();
	String line = "";
	while((line = input.readLine()) != null) {
	    sb2.append(line);
	}
	if(sb.equals(sb2)) {
	    System.out.println("Files match");
	}
	}catch(IOException e){
	    e.printStackTrace();
	}
    }
}
